package 代码记录.指定公司代码练习记录.西安.分值100分;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * @author zx
 * @create 2023-07-05 22:52
 * 参考人家的思路做出来的，期待二刷
 */
public class 数组合并or数组拼接 {//36--copy
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        String[] arr = new String[k];
        List<LinkedList<Character>> lists = new ArrayList<>(n);
        for(int i = 0;i < arr.length;i++){
            arr[i] = sc.next();
            LinkedList<Character> temp = new LinkedList<>();
            for(int j = 0;j < arr[i].length();j++){
                if(arr[i].charAt(j) >= '0' && arr[i].charAt(j) <='9'){
                    temp.add(arr[i].charAt(j));
                }else{
                    continue;
                }
            }
            lists.add(new LinkedList<>(temp));
        }
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (lists.size() > 0) {
            LinkedList<Character> list = lists.get(index);
            for (int i = 0; i < k; ++i) {
                if (list.isEmpty()) {
                    lists.remove(list);
                    --index;
                    break;
                }
                sb.append(list.getFirst());
                list.removeFirst();
                sb.append(",");
            }
            ++index;
            if (index >= lists.size()) {
                index = 0;
            }
        }
        System.out.println(sb.substring(0, sb.length() - 1));
    }
}
